package com.hong.spring.listener;

import com.alibaba.fastjson.JSONObject;
import com.hong.spring.api.IUserService;
import com.hong.spring.entity.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author: csh
 * @Date: 2021/3/16 11:14
 * @Description:用户监听
 */
@Log4j2
@Service("userListener")
public class UserListener implements MessageListener {

    @Autowired
    private IUserService userService;

    @Override
    public void onMessage(Message message) {
        try {
            log.info("传进来的数据为{}",JSONObject.toJSONString(message));
            if(null!=message){
                TextMessage tm = (TextMessage) message;
                String text = tm.getText();
                log.info("获取到的文本内容:"+text);
                User user = JSONObject.parseObject(text, User.class);
                userService.save(user);
            }
        }catch (Exception e){
            log.error("处理出错{}",e);
        }
    }
}
